package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.constant.ActivityStatus;
import com.campus.dao.ManagerClassMapper;
import com.campus.entry.ManagerActivity;
import com.campus.entry.ManagerClass;
import com.campus.entry.ManagerTeacher;
import com.campus.exception.BusinessException;
import com.campus.response.ResultCode;
import com.campus.service.ManagerActivityService;
import com.campus.service.ManagerClassService;
import com.campus.service.ManagerTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ManagerClassServiceImpl implements ManagerClassService {
    @Autowired
    private ManagerClassMapper managerClassMapper;
    @Autowired
    private ManagerActivityService managerActivityService;
    @Autowired
    private ManagerTeacherService managerTeacherService;

    /**
     * 创建一个教学班
     * @param managerClass
     */
    @Override
    public void createClass(ManagerClass managerClass) {
        Integer managerClassId=managerClass.getActivityId();
        Integer activityStatus = managerActivityService.getActivityStatus(managerClassId);
        if (Objects.isNull(activityStatus) ||!activityStatus.equals(ActivityStatus.PUBLISH_ACTIVITY)){
            throw new BusinessException(ResultCode.ERROR_NO_PUBLISH);
        }
        managerClassMapper.insert(managerClass);
    }

    /**
     * 更新教学班
     * @param managerClass
     */
    @Override
    public void updateClass(ManagerClass managerClass) {
        LambdaQueryWrapper<ManagerClass> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerClass::getId,managerClass.getId());
        managerClassMapper.update(managerClass, lambdaQueryWrapper);

    }

    @Override
    public void addTeacher(Integer classId,Integer teacherId) {
        ManagerTeacher teacherById = managerTeacherService.getTeacherById(teacherId);
        if (Objects.isNull(teacherById)){
            throw new BusinessException(ResultCode.ERROR_NO_TEACHER);
        }
        LambdaQueryWrapper<ManagerClass> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerClass::getId,classId);
        ManagerClass managerClass=new ManagerClass()
                .setTeacherId(teacherId);
        managerClassMapper.update(managerClass,lambdaQueryWrapper);
    }


}
